package com.ctshk.rpc.airexpress.service;

import com.ctshk.common.enums.Channel;
import com.ctshk.common.enums.IncreasePriceType;
import com.ctshk.rpc.airexpress.entity.AirportExpress;
import com.ctshk.rpc.airexpress.entity.AirportExpressIncreaseStrategyRule;

import java.math.BigDecimal;
import java.util.HashMap;

public class AirportExpressService {
    /**
     * 计算加幅价格
     *
     * @param airportExpress                     目标机场快线
     * @param airportExpressIncreaseStrategyRule 加幅规则
     * @param saleChannelId
     */
    public static HashMap<Integer, BigDecimal> buildIncreasePrice(AirportExpress airportExpress, AirportExpressIncreaseStrategyRule airportExpressIncreaseStrategyRule, Integer saleChannelId) {
        HashMap<Integer, BigDecimal> integerBigDecimalHashMap = new HashMap<>(2);
        // 单程票
        if (airportExpress.getTicketType() == 1) {
            // 单程票-成人票数值加幅
            BigDecimal adultOneWayPrice = BigDecimal.ZERO;
            if (airportExpressIncreaseStrategyRule.getAdultOneWayPriceType() == IncreasePriceType.NUMERIC.getCode()) {
                adultOneWayPrice = airportExpress.getAdultPrice().add(airportExpressIncreaseStrategyRule.getAdultOneWayPrice());
            }
            // 单程票-成人票百分比加幅
            if (airportExpressIncreaseStrategyRule.getAdultOneWayPriceType() == IncreasePriceType.PERCENT.getCode()) {
                BigDecimal per = airportExpressIncreaseStrategyRule.getAdultOneWayPrice().divide(new BigDecimal("100")).add(BigDecimal.ONE);
                adultOneWayPrice = airportExpress.getAdultPrice().multiply(per);
            }
//
            integerBigDecimalHashMap.put(0, buildProductSalePriceRoundUp(adultOneWayPrice, saleChannelId));
            // 单程票-儿童票数值或百分比加幅
            BigDecimal childrenOneWayPrice = BigDecimal.ZERO;
            if (airportExpressIncreaseStrategyRule.getChildrenOneWayPriceType() == IncreasePriceType.NUMERIC.getCode()) {
                childrenOneWayPrice = airportExpress.getChildrenPrice().add(airportExpressIncreaseStrategyRule.getChildrenOneWayPrice());
            } else if (airportExpressIncreaseStrategyRule.getChildrenOneWayPriceType() == IncreasePriceType.PERCENT.getCode()) {
                BigDecimal per = airportExpressIncreaseStrategyRule.getChildrenOneWayPrice().divide(new BigDecimal("100")).add(BigDecimal.ONE);
                childrenOneWayPrice = airportExpress.getChildrenPrice().multiply(per);
            }
            integerBigDecimalHashMap.put(1, buildProductSalePriceRoundUp(childrenOneWayPrice, saleChannelId));
        }
        // 来回票
        if (airportExpress.getTicketType() == 2) {
            // 来回票-成人票数值加幅
            BigDecimal adultRoundPrice = BigDecimal.ZERO;
            if (airportExpressIncreaseStrategyRule.getAdultRoundTripPriceType() == IncreasePriceType.NUMERIC.getCode()) {
                adultRoundPrice = airportExpress.getAdultPrice().add(airportExpressIncreaseStrategyRule.getAdultRoundTripPrice());
            }
            // 来回票-成人票百分比加幅
            if (airportExpressIncreaseStrategyRule.getAdultRoundTripPriceType() == IncreasePriceType.PERCENT.getCode()) {
                BigDecimal per = airportExpressIncreaseStrategyRule.getAdultRoundTripPrice().divide(new BigDecimal("100")).add(BigDecimal.ONE);
                adultRoundPrice = airportExpress.getAdultPrice().multiply(per);
            }
            integerBigDecimalHashMap.put(0, buildProductSalePriceRoundUp(adultRoundPrice, saleChannelId));
        }
        return integerBigDecimalHashMap;
    }

    /**
     * 按照舍入规则构建销售价格
     * @param salePrice
     * @param saleChannelId
     * @return
     */
    private static BigDecimal buildProductSalePriceRoundUp(BigDecimal salePrice, Integer saleChannelId) {
        if(Channel.AUTARKY.getCode().equals(saleChannelId) && Channel.AUTARKY.getCode().equals(saleChannelId)){
            String priceStr = salePrice.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString();
            return new BigDecimal(priceStr);
        }else{
            return salePrice.setScale(0, BigDecimal.ROUND_UP);
        }
    }
}
